//
//  HECartItemCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/10/26.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HECartItemCell.h"
#import "CountView.h"

@interface HECartItemCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UIImageView *goodsimgView;


@property (nonatomic, strong) UIImageView *selectBtn;

@property (nonatomic, strong) UILabel *titleLb;
@property (nonatomic, strong) UILabel *priceLb;
@property (nonatomic, strong) UILabel *skuLb;

@property (nonatomic, strong) UIButton *cutBtn;
@property (nonatomic, strong) UIButton *addBtn;
@property (nonatomic, strong) UITextField *numTf;

/**
 最大库存
 */
@property (nonatomic, assign)NSInteger goodstock;

@end

@implementation HECartItemCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        _goodstock = 100000000;
        
        [self initUI];
    }
    return self;;
}

- (void)initUI {


    UIView *warp = [UIView new];
    warp.backgroundColor = KWhiteColor;
    [self.contentView addSubview:warp];
    
    [warp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
        
        make.height.mas_greaterThanOrEqualTo(105);
        make.bottom.equalTo(self.contentView);
    }];
    
    UIView *line = [UIView new];
    line.backgroundColor = CSplitLineColor;
    [warp addSubview:line];

    // 勾选
    _selectBtn = [UIImageView new];
    _selectBtn.image = IMAGE_NAMED(@"check_box_unsel");
    _selectBtn.userInteractionEnabled = YES;
    [warp addSubview:_selectBtn];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectGoodsClick)];
    [_selectBtn addGestureRecognizer:tap];
    
    // 商品图片
    _goodsimgView = [UIImageView new];
    _goodsimgView.contentMode = UIViewContentModeScaleAspectFill;
    [warp addSubview:_goodsimgView];
    
    // 商品名称
    _titleLb = [UILabel new];
    _titleLb.text = @"平台自营";
    _titleLb.font = DYFont(16);
    [warp addSubview:_titleLb];
    
    // 规格
    _skuLb = [UILabel new];
    _skuLb.text = @"规格：但是成都市成都市成都市成都市";
    _skuLb.font = DYFont(14);
    _skuLb.numberOfLines = 0;
    _skuLb.textColor = UIColor.lightGrayColor;
    [warp addSubview:_skuLb];
    
    // 价格
    _priceLb = [UILabel new];
    _priceLb.text = @"￥111";
    _priceLb.font = DYBlodFont(16);
    _priceLb.textColor = KHexColor(@"#FF5757");
    [warp addSubview:_priceLb];
    
//    CountView *skuCountView = [[CountView alloc] initWithFrame:CGRectMake(KScreenWidth - 15 -80, 0, 80, 22) withLeftImage:@"shop_car_reduce" rightImage:@"shop_car_add"];
//    [warp addSubview:skuCountView];
    
    
    // 增加
    _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_addBtn setImage:IMAGE_NAMED(@"shop_car_add") forState:UIControlStateNormal];
    _addBtn.qmui_borderPosition = QMUIViewBorderPositionTop|QMUIViewBorderPositionBottom|QMUIViewBorderPositionRight;
    _addBtn.qmui_width = 1;
    _addBtn.qmui_borderWidth = 0.5;
    _addBtn.qmui_borderColor = CSplitLineColor;
    [warp addSubview:_addBtn];
    [_addBtn addTarget:self action:@selector(addBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [_addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_priceLb);
        make.right.equalTo(warp.mas_right).mas_offset(-10);
        make.width.height.mas_offset(23);
    }];
    
    _numTf = [UITextField new];
    _numTf.qmui_borderPosition = QMUIViewBorderPositionTop|QMUIViewBorderPositionBottom|QMUIViewBorderPositionRight|QMUIViewBorderPositionLeft;
    _numTf.qmui_width = 1;
    _numTf.qmui_borderWidth = 0.5;
    _numTf.qmui_borderColor = CSplitLineColor;
    _numTf.font = DYFont(15);
    _numTf.textAlignment = NSTextAlignmentCenter;
    _numTf.keyboardType = UIKeyboardTypeNumberPad;
    _numTf.enabled = NO;
    _numTf.delegate = self;
    [warp addSubview:_numTf];
    [self setTFValue]; // 设置默认数量

    
    [_numTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_priceLb);
        make.right.equalTo(_addBtn.mas_left);
        make.width.mas_offset(35);
        make.height.mas_offset(23);
    }];

     //减少
    _cutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_cutBtn setImage:IMAGE_NAMED(@"shop_car_reduce") forState:UIControlStateNormal];
    _cutBtn.qmui_borderPosition = QMUIViewBorderPositionTop|QMUIViewBorderPositionBottom|QMUIViewBorderPositionLeft;
    _cutBtn.qmui_width = 1;
    _cutBtn.qmui_borderWidth = 0.5;
    _cutBtn.qmui_borderColor = CSplitLineColor;
    [warp addSubview:_cutBtn];
    [_cutBtn addTarget:self action:@selector(cutBtnClick) forControlEvents:UIControlEventTouchUpInside];

    [_cutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
      make.centerY.equalTo(_priceLb);
      make.right.equalTo(_numTf.mas_left);
      make.width.height.mas_offset(23);
    }];
    
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(warp);
        make.left.equalTo(warp).mas_offset(30);
        make.right.equalTo(warp).mas_offset(-30);
        make.height.mas_offset(0.5);
    }];
    
    [_selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(warp);
        make.left.equalTo(warp).mas_offset(13);
        make.width.height.mas_offset(25);
    }];
    
    [_goodsimgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(warp);
        make.left.equalTo(_selectBtn.mas_right).mas_offset(10);
        make.width.height.mas_offset(85);
    }];
    
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(warp).mas_offset(10);
        make.left.equalTo(_goodsimgView.mas_right).mas_offset(10);
        make.right.equalTo(warp).mas_offset(-15);
    }];
    
    [_skuLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLb.mas_bottom).mas_offset(5);
        make.left.equalTo(_titleLb.mas_left);
        make.right.equalTo(warp).mas_offset(-15);
    }];
    
    [_priceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_skuLb.mas_bottom).mas_offset(5);
        make.left.equalTo(_titleLb.mas_left);
        make.right.equalTo(_cutBtn.mas_left).mas_offset(-5);
        make.bottom.equalTo(warp.mas_bottom).mas_offset(-10);
    }];
    
    [_goodsimgView hg_setAllCornerWithCornerRadius:6.0f];

//    [warp LX_SetShadowPathWith:[UIColor greenColor] shadowOpacity:0.3 shadowRadius:2 shadowSide:LXShadowPathLeft shadowPathWidth:10];
    
}

- (void)setTFValue {
    _numTf.text = [NSString stringWithFormat:@"%zd", _model.quantity];
}

#pragma mark -
- (void)addBtnClick {
    [_numTf resignFirstResponder];
    NSInteger count = [_numTf.text integerValue] + 1;
    if (count>_goodstock) {
        [Dialog toast:@"超出库存了~" delay:2.0];
        return;
    }
    
    _model.quantity = count;
    [self setTFValue];
    
    if ([self.delegate respondsToSelector:@selector(updateGoodsQuantity:withNum:changheType:)]) {
        [self.delegate updateGoodsQuantity:self withNum:_model.quantity changheType:HECartTotalChangeTypeAdd];
    }
}

- (void)cutBtnClick {
    [_numTf resignFirstResponder];
    NSInteger count = [_numTf.text integerValue] - 1;
    if (count <= 0) {
        return;
    }
    
    _model.quantity = count;
    if (count == 1) {
//        [self.cutBtn setImage:[UIImage imageNamed:_leftImage] forState:UIControlStateNormal];
    }
    [self setTFValue];
    
    if ([self.delegate respondsToSelector:@selector(updateGoodsQuantity:withNum:changheType:)]) {
          [self.delegate updateGoodsQuantity:self withNum:_model.quantity changheType:HECartTotalChangeTypeCut];
    }
}

- (void)selectGoodsClick {
    _model.isChoose = !_model.isChoose;
    [self changeSelectBtnImage];
    
    if ([self.delegate respondsToSelector:@selector(goodsCellSelect:withModel:)]) {
        [self.delegate goodsCellSelect:self withModel:_model];
    }
}

- (void)changeSelectBtnImage {
    if (_model.isChoose) {
        _selectBtn.image = IMAGE_NAMED(@"check_box_sel");
    }else {
        _selectBtn.image = IMAGE_NAMED(@"check_box_unsel");
    }
}

#pragma mark -- UITextFieldDelegate
-(void)textFieldDidEndEditing:(UITextField *)textField{
    DLog(@"%@", textField.text);
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.numTf resignFirstResponder];
    return YES;
}

#pragma mark -
- (void)setModel:(HEShopCarModel *)model {
    _model = model;
    
    [_goodsimgView sd_setImageWithURL:[NSURL URLWithString:model.productPic] placeholderImage:IMAGE_NAMED(@"noImage")];
    
    _titleLb.text = model.productName;
    _skuLb.text = model.productAttr;
    _priceLb.text = [NSString stringWithFormat:@"￥%.2f", model.price];
    [self setTFValue];
    [self changeSelectBtnImage];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
